package com.kasax.plugin.app.visitor.injectTime

import com.kasax.plugin.app.LogUtils
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.Opcodes

/**
 * 需结合ClassVisitor遍历方法，检测@ASMTest注解，构造MethodVisitor传递needInject标志。
   visitCode方法在方法体开始时调用，插入打印指令即可。
 */
class TimeMethodVisitor extends MethodVisitor {
    private String TAG = "TimeMethodVisitor"

    TimeMethodVisitor(MethodVisitor methodVisitor) {
        super(Opcodes.ASM7, methodVisitor)
    }

    @Override
    void visitCode() {
        super.visitCode()
        LogUtils.d(TAG, "visitCode start")
        // 插入打印指令
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;")
        mv.visitLdcInsn("ASMTest Method start!")
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false)
        LogUtils.d(TAG, "visitCode complete")
    }
}